package com.hnkywl.sts.dao.kcgl;

import com.hnkywl.sts.dao.sys.YgDao;
import com.hnkywl.sts.entity.organ.Ck;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.*;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-5-3
 * Time: 上午10:59
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class CkshDao extends SimpleHibernateTemplate<Xsd, Long> {
    private YgDao ygDao;

    //出库审核
    public Page<Xsd> findAllCkshList(String wzbm, String xsdh, Long khId, Long ckId, Date zdrqStart, Date zdrqEnd, Long xsgsId, String[] xslx, String[] zt, String yck, String yps, int pageNo, int pageSize, Date cksjStart, Date cksjEnd) {
        Page<Xsd> page = new Page<Xsd>();
        page.setAutoCount(false);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Xsmx.class);

        detachedCriteria.createAlias("jtwz", "jtwz", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("xsd.id").as("xsd.id")))
                .add(Restrictions.or(Restrictions.like("jtwz.code", wzbm, MatchMode.START), Restrictions.like("jtwz.oldcode", wzbm, MatchMode.START)));

        Criteria countCriteria = getSession().createCriteria(this.entityClass)
                .setProjection(Projections.rowCount());
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("gc", "gc", CriteriaSpecification.LEFT_JOIN)
                .createAlias("thck", "thck", CriteriaSpecification.LEFT_JOIN)
                .createAlias("fyzr", "fyzr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsgs", "xsgs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsbm", "xsbm", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsr", "xsr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("ckr", "ckr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsmxList", "xsmxList")
                .setProjection(Projections.projectionList()
                        .add(Projections.sum("xsmxList.ck_js").as("ck_js"))
                        .add(Projections.sum("xsmxList.ck_sbsl").as("ck_sbsl"))
                        .add(Projections.sum("xsmxList.sbsl").as("xssl"))
                        .add(Projections.sum("xsmxList.hsj").as("xsje"))
                        .add(Projections.sum("xsmxList.bhsj").as("xsbhsje"))
                        .add(Projections.groupProperty("id").as("id"))
                        .add(Projections.groupProperty("xsht.id").as("xsht_id"))
                        .add(Projections.sqlGroupProjection("xsdh as xsdh", "xsdh", new String[]{"xsdh"}, new Type[]{Hibernate.STRING}))
                        .add(Projections.sqlGroupProjection("fhsj as fhsj", "fhsj", new String[]{"fhsj"}, new Type[]{Hibernate.DATE}))
                        .add(Projections.sqlGroupProjection("sxsj as sxsj", "sxsj", new String[]{"sxsj"}, new Type[]{Hibernate.DATE}))
                        .add(Projections.groupProperty("kh.qc").as("kh_qc"))
                        .add(Projections.groupProperty("gc.mc").as("gc_mc"))
                        .add(Projections.groupProperty("thck.mc").as("thck_mc"))
                        .add(Projections.groupProperty("fyzr.z").as("fyzr_z"))
                        .add(Projections.groupProperty("dsyfje").as("dsyfje"))
                        .add(Projections.sqlGroupProjection("xslx as xslx", "xslx", new String[]{"xslx"}, new Type[]{Hibernate.STRING}))
                        .add(Projections.sqlGroupProjection("this_.yck as yck", "this_.yck", new String[]{"yck"}, new Type[]{Hibernate.BOOLEAN}))
                        .add(Projections.sqlGroupProjection("this_.yps as yps", "this_.yps", new String[]{"yps"}, new Type[]{Hibernate.BOOLEAN}))
                        .add(Projections.groupProperty("xsgs.qc").as("xsgs_qc"))
                        .add(Projections.groupProperty("xsbm.qc").as("xsbm_qc"))
                        .add(Projections.groupProperty("xsr.xm").as("xsr_xm"))
                        .add(Projections.groupProperty("createUser.xm").as("zdr"))
                        .add(Projections.groupProperty("ckshbz").as("ckshbz"))
                        .add(Projections.sqlGroupProjection("this_.zt as zt", "this_.zt", new String[]{"zt"}, new Type[]{Hibernate.STRING}))
                        .add(Projections.groupProperty("dateCreated").as("date_created"))
                        .add(Projections.groupProperty("ckr.xm").as("ckr"))
                        .add(Projections.sqlGroupProjection("cksj as cksj", "cksj", new String[]{"cksj"}, new Type[]{Hibernate.DATE}))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (StringUtils.isNotEmpty(wzbm)) {
            criteria.add(Property.forName("id").in(detachedCriteria));
            countCriteria.add(Property.forName("id").in(detachedCriteria));
        }
        if (StringUtils.isNotEmpty(xsdh)) {
            criteria.add(Property.forName("xsdh").like(xsdh, MatchMode.START));
            countCriteria.add(Property.forName("xsdh").like(xsdh, MatchMode.START));
        }
        if (xsgsId != null) {
            criteria.add(Restrictions.eq("xsgs.id", xsgsId));
            countCriteria.add(Restrictions.eq("xsgs.id", xsgsId));
        }
        if (khId != null) {
            criteria.add(Restrictions.eq("kh.id", khId));
            countCriteria.add(Restrictions.eq("kh.id", khId));
        }
        if (ckId != null) {
            criteria.add(Restrictions.eq("thck.id", ckId));
            countCriteria.add(Restrictions.eq("thck.id", ckId));
        }

        if (zdrqStart != null) {
            criteria.add(Restrictions.ge("dateCreated", sDaysAfterNowDate(zdrqStart)));
            countCriteria.add(Restrictions.ge("dateCreated", sDaysAfterNowDate(zdrqStart)));
        } else {
            //得到月初
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date start = calendar.getTime();
            criteria.add(Restrictions.ge("dateCreated", start));
            countCriteria.add(Restrictions.ge("dateCreated", start));
        }
        if (zdrqEnd != null) {
            criteria.add(Restrictions.le("dateCreated", jDaysAfterNowDate(zdrqEnd)));
            countCriteria.add(Restrictions.le("dateCreated", jDaysAfterNowDate(zdrqEnd)));
        } else {
            //得到月末
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date end = calendar.getTime();
            criteria.add(Restrictions.le("dateCreated", end));
            countCriteria.add(Restrictions.le("dateCreated", end));
        }

        if (cksjStart != null) {
            criteria.add(Restrictions.ge("cksj", sDaysAfterNowDate(cksjStart)));
            countCriteria.add(Restrictions.ge("cksj", sDaysAfterNowDate(cksjStart)));
        }

        if (cksjEnd != null) {
            criteria.add(Restrictions.le("cksj", jDaysAfterNowDate(cksjEnd)));
            countCriteria.add(Restrictions.le("cksj", jDaysAfterNowDate(cksjEnd)));
        }

        if (zt != null && zt.length != 0) {
            criteria.add(Restrictions.in("zt", zt));
            countCriteria.add(Restrictions.in("zt", zt));
        }
        if (xslx != null && xslx.length != 0) {
            criteria.add(Restrictions.in("xslx", xslx));
            countCriteria.add(Restrictions.in("xslx", xslx));
        }
        if (yck != null && yck.equals("yck")) {
            criteria.add(Restrictions.eq("yck", true));
            countCriteria.add(Restrictions.eq("yck", true));
        } else if (yck != null && yck.equals("wck")) {
            criteria.add(Restrictions.eq("yck", false));
            countCriteria.add(Restrictions.eq("yck", false));
        }
        if (yps != null && yps.equals("yps")) {
            criteria.add(Restrictions.eq("yps", true));
            countCriteria.add(Restrictions.eq("yps", true));
        } else if (yps != null && yps.equals("wps")) {
            criteria.add(Restrictions.and(Restrictions.eq("yps", false), Restrictions.eq("fyzr.id", 43l)));
            countCriteria.add(Restrictions.and(Restrictions.eq("yps", false), Restrictions.eq("fyzr.id", 43l)));
        }


        criteria.add(Restrictions.eq("cwsh", true));
        countCriteria.add(Restrictions.eq("cwsh", true));
        criteria.add(Restrictions.eq("sftzd", false));
        countCriteria.add(Restrictions.eq("sftzd", false));
        criteria.addOrder(Property.forName("cksj").desc());
        criteria.addOrder(Property.forName("dateCreated").desc());
        createKcDataRegion(criteria);
        createKcDataRegion(countCriteria);
        Page list = this.findByCriteria(page, criteria);
        Long totalCount = (Long) countCriteria.uniqueResult();
        list.setTotalCount(totalCount.intValue());
        return list;
    }

    //数量审核
    public Page<Xsd> findAllSlslList(String wzbm, String xsdh, Long khId, Long ckId, Date zdrqStart, Date zdrqEnd, Long xsgsId, String[] xslx, String[] zt, String yck, String yps, int pageNo, int pageSize) {
        Page<Xsd> page = new Page<Xsd>();
        page.setAutoCount(false);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Xsmx.class);

        detachedCriteria.createAlias("jtwz", "jtwz", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("xsd.id").as("xsd.id")))
                .add(Restrictions.or(Restrictions.like("jtwz.code", wzbm, MatchMode.START), Restrictions.like("jtwz.oldcode", wzbm, MatchMode.START)));

        Criteria countCriteria = getSession().createCriteria(this.entityClass)
                .setProjection(Projections.rowCount());
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("gc", "gc", CriteriaSpecification.LEFT_JOIN)
                .createAlias("thck", "thck", CriteriaSpecification.LEFT_JOIN)
                .createAlias("fyzr", "fyzr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsgs", "xsgs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsbm", "xsbm", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsr", "xsr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("ckr", "ckr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsmxList", "xsmxList")
                .setProjection(Projections.projectionList()
                        .add(Projections.sum("xsmxList.ck_js").as("ck_js"))
                        .add(Projections.sum("xsmxList.ck_sbsl").as("ck_sbsl"))
                        .add(Projections.sum("xsmxList.sbsl").as("xssl"))
                        .add(Projections.sum("xsmxList.hsj").as("xsje"))
                        .add(Projections.sum("xsmxList.bhsj").as("xsbhsje"))
                        .add(Projections.groupProperty("id").as("id"))
                        .add(Projections.groupProperty("xsht.id").as("xsht_id"))
                        .add(Projections.sqlGroupProjection("xsdh as xsdh", "xsdh", new String[]{"xsdh"}, new Type[]{Hibernate.STRING}))
                        .add(Projections.sqlGroupProjection("fhsj as fhsj", "fhsj", new String[]{"fhsj"}, new Type[]{Hibernate.DATE}))
                        .add(Projections.sqlGroupProjection("sxsj as sxsj", "sxsj", new String[]{"sxsj"}, new Type[]{Hibernate.DATE}))
                        .add(Projections.groupProperty("kh.qc").as("kh_qc"))
                        .add(Projections.groupProperty("gc.mc").as("gc_mc"))
                        .add(Projections.groupProperty("thck.mc").as("thck_mc"))
                        .add(Projections.groupProperty("fyzr.z").as("fyzr_z"))
                        .add(Projections.groupProperty("dsyfje").as("dsyfje"))
                        .add(Projections.sqlGroupProjection("xslx as xslx", "xslx", new String[]{"xslx"}, new Type[]{Hibernate.STRING}))
                        .add(Projections.sqlGroupProjection("this_.yck as yck", "this_.yck", new String[]{"yck"}, new Type[]{Hibernate.BOOLEAN}))
                        .add(Projections.sqlGroupProjection("this_.yps as yps", "this_.yps", new String[]{"yps"}, new Type[]{Hibernate.BOOLEAN}))
                        .add(Projections.groupProperty("xsgs.qc").as("xsgs_qc"))
                        .add(Projections.groupProperty("xsbm.qc").as("xsbm_qc"))
                        .add(Projections.groupProperty("xsr.xm").as("xsr_xm"))
                        .add(Projections.groupProperty("createUser.xm").as("zdr"))
                        .add(Projections.groupProperty("ckshbz").as("ckshbz"))
                        .add(Projections.sqlGroupProjection("this_.zt as zt", "this_.zt", new String[]{"zt"}, new Type[]{Hibernate.STRING}))
                        .add(Projections.groupProperty("dateCreated").as("date_created"))
                        .add(Projections.groupProperty("ckr.xm").as("ckr"))
                        .add(Projections.groupProperty("cksj").as("cksj"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (StringUtils.isNotEmpty(wzbm)) {
            criteria.add(Property.forName("id").in(detachedCriteria));
            countCriteria.add(Property.forName("id").in(detachedCriteria));
        }
        if (StringUtils.isNotEmpty(xsdh)) {
            criteria.add(Property.forName("xsdh").like(xsdh, MatchMode.START));
            countCriteria.add(Property.forName("xsdh").like(xsdh, MatchMode.START));
        }
        if (xsgsId != null) {
            criteria.add(Restrictions.eq("xsgs.id", xsgsId));
            countCriteria.add(Restrictions.eq("xsgs.id", xsgsId));
        }
        if (khId != null) {
            criteria.add(Restrictions.eq("kh.id", khId));
            countCriteria.add(Restrictions.eq("kh.id", khId));
        }
        if (ckId != null) {
            criteria.add(Restrictions.eq("thck.id", ckId));
            countCriteria.add(Restrictions.eq("thck.id", ckId));
        }

        if (zdrqStart != null) {
            criteria.add(Restrictions.ge("dateCreated", sDaysAfterNowDate(zdrqStart)));
            countCriteria.add(Restrictions.ge("dateCreated", sDaysAfterNowDate(zdrqStart)));
        } else {
            //得到月初
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date start = calendar.getTime();
            criteria.add(Restrictions.ge("dateCreated", start));
            countCriteria.add(Restrictions.ge("dateCreated", start));
        }
        if (zdrqEnd != null) {
            criteria.add(Restrictions.le("dateCreated", jDaysAfterNowDate(zdrqEnd)));
            countCriteria.add(Restrictions.le("dateCreated", jDaysAfterNowDate(zdrqEnd)));
        } else {
            //得到月末
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date end = calendar.getTime();
            criteria.add(Restrictions.le("dateCreated", end));
            countCriteria.add(Restrictions.le("dateCreated", end));
        }

        if (zt != null && zt.length != 0) {
            criteria.add(Restrictions.in("zt", zt));
            countCriteria.add(Restrictions.in("zt", zt));
        }
        if (xslx != null && xslx.length != 0) {
            criteria.add(Restrictions.in("xslx", xslx));
            countCriteria.add(Restrictions.in("xslx", xslx));
        }
        if (yck != null && yck.equals("yck")) {
            criteria.add(Restrictions.eq("yck", true));
            countCriteria.add(Restrictions.eq("yck", true));
        } else if (yck != null && yck.equals("wck")) {
            criteria.add(Restrictions.eq("yck", false));
            countCriteria.add(Restrictions.eq("yck", false));
        }
        if (yps != null && yps.equals("yps")) {
            criteria.add(Restrictions.eq("yps", true));
            countCriteria.add(Restrictions.eq("yps", true));
        } else if (yps != null && yps.equals("wps")) {
            criteria.add(Restrictions.and(Restrictions.eq("yps", false), Restrictions.eq("fyzr.id", 43l)));
            countCriteria.add(Restrictions.and(Restrictions.eq("yps", false), Restrictions.eq("fyzr.id", 43l)));
        }


        criteria.add(Restrictions.eq("cwsh", true));
        countCriteria.add(Restrictions.eq("cwsh", true));
        criteria.add(Restrictions.eq("sftzd", false));
        countCriteria.add(Restrictions.eq("sftzd", false));
        criteria.addOrder(Property.forName("cksj").desc());
        criteria.addOrder(Property.forName("dateCreated").desc());
        createKcDataRegion(criteria);
        createKcDataRegion(countCriteria);
        Page list = this.findByCriteria(page, criteria);
        Long totalCount = (Long) countCriteria.uniqueResult();
        list.setTotalCount(totalCount.intValue());
        return list;
    }

    /**
     * 首页显示出库单数
     *
     * @return
     */
    public String findCkdList() {
        Criteria countCriteria = getSession().createCriteria(this.entityClass)
                .setProjection(Projections.rowCount());
        //得到月初
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date start = calendar.getTime();
        countCriteria.add(Restrictions.ge("dateCreated", start));
        //得到月末
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date end = calendar.getTime();
        countCriteria.add(Restrictions.le("dateCreated", end));
        countCriteria.add(Restrictions.eq("yck", false));
        countCriteria.add(Restrictions.eq("cwsh", true));
        countCriteria.add(Restrictions.eq("sftzd", false));
        createKcDataRegion(countCriteria);
        Long totalCount = (Long) countCriteria.uniqueResult();
        return totalCount.toString();
    }


    //   当前日期加减n天后的日期，返回String   (yyyy-mm-dd)
    public Date jDaysAfterNowDate(Date date) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.set(java.util.Calendar.HOUR_OF_DAY, 23);
        rightNow.set(java.util.Calendar.MINUTE, 59);
        rightNow.set(java.util.Calendar.SECOND, 59);
        return rightNow.getTime();
    }

    //   当前日期加减n天后的日期，返回String   (yyyy-mm-dd)
    public Date sDaysAfterNowDate(Date date) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.set(java.util.Calendar.HOUR_OF_DAY, 0);
        rightNow.set(java.util.Calendar.MINUTE, 0);
        rightNow.set(java.util.Calendar.SECOND, 0);
        return rightNow.getTime();
    }

    public List<Map> findSumCkshList(String wzbm, String xsdh, Long khId, Long ckId, Date zdrqStart, Date zdrqEnd, Long xsgsId, String[] xslx, String[] zt, String yck, String yps, Date cksjStart, Date cksjEnd) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Xsmx.class);
        detachedCriteria.createAlias("jtwz", "jtwz", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("xsd.id").as("xsd.id")))
                .add(Restrictions.like("jtwz.code", wzbm, MatchMode.START));
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("gc", "gc", CriteriaSpecification.LEFT_JOIN)
                .createAlias("thck", "thck", CriteriaSpecification.LEFT_JOIN)
                .createAlias("fyzr", "fyzr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsgs", "xsgs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsbm", "xsbm", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsr", "xsr", CriteriaSpecification.LEFT_JOIN)
                .createAlias("xsmxList", "xsmxList")
                .setProjection(Projections.projectionList()
                        .add(Projections.sum("xsmxList.ck_js").as("ck_js"))
                        .add(Projections.sum("xsmxList.ck_sbsl").as("ck_sbsl"))
                        .add(Projections.sum("xsmxList.sbsl").as("xssl"))
                        .add(Projections.sum("xsmxList.hsj").as("xsje"))
                        .add(Projections.sum("xsmxList.bhsj").as("xsbhsje"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (StringUtils.isNotEmpty(wzbm)) {
            criteria.add(Property.forName("id").in(detachedCriteria));
        }
        if (StringUtils.isNotEmpty(xsdh)) {
            criteria.add(Property.forName("xsdh").like(xsdh, MatchMode.START));
        }
        if (xsgsId != null) {
            criteria.add(Restrictions.eq("xsgs.id", xsgsId));
        }
        if (khId != null) {
            criteria.add(Restrictions.eq("kh.id", khId));
        }
        if (ckId != null) {
            criteria.add(Restrictions.eq("thck.id", ckId));
        }
        if (zdrqStart != null) {
            criteria.add(Restrictions.ge("dateCreated", sDaysAfterNowDate(zdrqStart)));
        } else {
            //得到月初
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date start = calendar.getTime();
            criteria.add(Restrictions.ge("dateCreated", start));
        }
        if (zdrqEnd != null) {
            criteria.add(Restrictions.le("dateCreated", jDaysAfterNowDate(zdrqEnd)));
        } else {
            //得到月末
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date end = calendar.getTime();
            criteria.add(Restrictions.le("dateCreated", end));
        }
        if (cksjStart != null) {
            criteria.add(Restrictions.ge("cksj", sDaysAfterNowDate(cksjStart)));
        }
        if (cksjEnd != null) {
            criteria.add(Restrictions.le("cksj", jDaysAfterNowDate(cksjEnd)));
        }
        if (zt != null && zt.length != 0) {
            criteria.add(Restrictions.in("zt", zt));
        }
        if (xslx != null && xslx.length != 0) {
            criteria.add(Restrictions.in("xslx", xslx));
        }
        if (yck != null && yck.equals("yck")) {
            criteria.add(Restrictions.eq("yck", true));
        } else if (yck != null && yck.equals("wck")) {
            criteria.add(Restrictions.eq("yck", false));
        }
        if (yps != null && yps.equals("yps")) {
            criteria.add(Restrictions.eq("yps", true));
        } else if (yps != null && yps.equals("wps")) {
            criteria.add(Restrictions.and(Restrictions.eq("yps", false), Restrictions.eq("fyzr.id", 43l)));
        }
        criteria.add(Restrictions.eq("cwsh", true));
        criteria.add(Restrictions.eq("sftzd", false));
        criteria.addOrder(Property.forName("cksj").desc());
        criteria.addOrder(Property.forName("dateCreated").desc());
        createKcDataRegion(criteria);

        return criteria.list();
    }

    private void createKcDataRegion(Criteria criteria) {
        List<Ck> ckList = this.ygDao.get(YgService.getCurrentYg().getId()).getCkList();
        if (ckList != null && ckList.size() > 0) {
            criteria.createAlias("createUser", "createUser", CriteriaSpecification.LEFT_JOIN);
            switch (YgService.getCurrentYgCxfw()) {
                case 0:
                    break;
                case 1:
                    criteria.add(Restrictions.or(Restrictions.eq("createOrgan.id", YgService.getCurrentYgZgGsId()), Restrictions.in("thck", ckList)));
                    break;
                case 2:
                    criteria.add(Restrictions.or(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()), Restrictions.in("thck", ckList)));
                    break;
                case 3:
                    criteria.add(Restrictions.or(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()), Restrictions.in("thck", ckList)));
                    criteria.add(Restrictions.or(Restrictions.eq("createUser.id", YgService.getCurrentYg().getId()), Restrictions.in("thck", ckList)));
                    break;
            }
        } else {
            criteria.createAlias("createUser", "createUser", CriteriaSpecification.LEFT_JOIN);
            switch (YgService.getCurrentYgCxfw()) {
                case 0:
                    break;
                case 1:
                    criteria.add(Restrictions.eq("createOrgan.id", YgService.getCurrentYgZgGsId()));
                    break;
                case 2:
                    criteria.add(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()));
                    break;
                case 3:
                    criteria.add(Restrictions.eq("createDepartment.id", YgService.getCurrentYgGsId()));
                    criteria.add(Restrictions.eq("createUser.id", YgService.getCurrentYg().getId()));
                    break;
            }
        }
    }

    public List<Map> findCkshList(Long xsdId) {
        Criteria criteria = getSession().createCriteria(Xsmx.class)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jzfs", "jzfs", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("kh.id").as("kh.id"))
                        .add(Property.forName("kh.khbm").as("kh.khbm"))
                        .add(Property.forName("kh.kkdsl").as("kkdsl"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Projections.sqlProjection("wzgg2_.cz||' '||wzgg2_.ggxh as \"wzgg.ggxh\"", new String[]{"wzgg.ggxh"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("cd.id").as("cd.id"))
                        .add(Property.forName("cd.z").as("cd.z"))
                        .add(Property.forName("js").as("js"))
                        .add(Property.forName("sbsl").as("sbsl"))
                        .add(Property.forName("ck_js").as("ck_js"))
                        .add(Property.forName("ck_sbsl").as("ck_sbsl"))
                        .add(Property.forName("jzfs.id").as("jzfs.id"))
                        .add(Property.forName("jzfs.z").as("jzfs.z"))
                        .add(Property.forName("hsj").as("hsj"))
                        .add(Property.forName("bhsj").as("bhsj"))
                        .add(Projections.sqlProjection("this_.bhsj*this_.ck_sbsl as \"bhse\"", new String[]{"bhse"}, new Type[]{Hibernate.DOUBLE}))
                        .add(Property.forName("bz").as("bz"))
                ).add(Restrictions.eq("xsd.id", xsdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }

    public List<Map> findZddhXsmxList(Long xsdId) {
        Criteria criteria = getSession().createCriteria(Xsmx.class)
                .createAlias("jzfs", "jzfs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jtwz", "wzgg", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("wzgg.id").as("wzgg.id"))
                        .add(Property.forName("wzgg.code").as("wzgg.code"))
                        .add(Property.forName("wzgg.name").as("wzgg.name"))
                        .add(Projections.sqlProjection("wzgg2_.cz||' '||wzgg2_.ggxh as \"wzgg.ggxh\"", new String[]{"wzgg.ggxh"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("wzgg.dw").as("wzgg.dw"))
                        .add(Property.forName("js").as("js"))
                        .add(Property.forName("sbsl").as("sbsl"))
                        .add(Property.forName("jzfs.id").as("jzfs.id"))
                        .add(Property.forName("jzfs.z").as("jzfs.z"))
                        .add(Property.forName("hsj").as("hsj"))
                        .add(Property.forName("bhsj").as("bhsj"))
                        .add(Property.forName("bz").as("bz"))
                ).add(Restrictions.eq("xsd.id", xsdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return (List<Map>) criteria.list();
    }

    @Autowired
    public void setYgDao(YgDao ygDao) {
        this.ygDao = ygDao;
    }
}
